package com.shadow.baiqun.delegate.mine;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.shadow.baiqun.R;
import com.shadow.baiqun.R2;
import com.shadow.baiqun.config.SpKey;
import com.shadow.baiqun.event.CheckVersionEvent;
import com.shadow.baiqun.event.LogoutEvent;
import com.shadow.core.delegate.bottom.BottomItemDelegate;
import com.shadow.core.util.app.AppUtils;
import com.shadow.core.util.storage.ShadowPreference;

import org.greenrobot.eventbus.EventBus;

import java.text.MessageFormat;

import butterknife.BindView;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * @author zy
 * on 2018/5/19
 */
@RuntimePermissions
public class MineDelegate extends BottomItemDelegate {
    @BindView(R2.id.tv_account)
    TextView mTvAccount;
    @BindView(R2.id.tv_version)
    TextView mTvVersion;

    @Override
    public Object setLayout() {
        return R.layout.delegate_mine;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvAccount.setText(ShadowPreference.getCustomAppProfile(SpKey.KEY_USER_ACCOUNT));
        mTvVersion.setText(MessageFormat.format("版本检测 v{0}", AppUtils.getVersion(getContext())));
    }

    @OnClick(R2.id.tv_logout)
    public void logout() {
        EventBus.getDefault().post(new LogoutEvent());
    }
    @NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE})
    public void getMulti() {
        Toast.makeText(getContext(), "getMulti", Toast.LENGTH_SHORT).show();
    }
    @OnClick(R2.id.tv_version)
    public void onCheckUpdate(){
        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            MineDelegatePermissionsDispatcher.getMultiWithCheck(this);
        }
        EventBus.getDefault().post(new CheckVersionEvent());
    }
    @OnClick(R2.id.tv_pre_receive_goods)
    public void onReceiveGoods() {
        //预收货列表
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MineDelegatePermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
